package com.ncloud.resman.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.entity.IpAddress;

@Mapper
public interface IpAddressMapper {
	@Select("SELECT CONCAT(id,'') as id, net_type, "
			+ "(select t.dict_value from sys_dictionary t where dictionary_id = 'net_type' and t.dict_key=net_type) as net_type_name,"
			+ "ip_segment, ip, state, apply_person, purpose,"
			+ "CONCAT(create_user,'') as create_user,"
			+ "date_format(create_date, '%Y-%m-%d %H:%i:%s' ) as create_date, CONCAT(update_user,'') as update_user, "
			+ "date_format(update_date, '%Y-%m-%d %H:%i:%s' ) as update_date, remarks "
			+ "FROM res_ip_address "
			+ "where (#{0}=0 or net_type = #{0}) "
			+ "and (#{1}=0 or ip_segment = #{1}) "
			+ "and (#{2}=2 or state = #{2}) "
			+ "and (#{3}='' or ip = #{3}) ")
	@Results({
		@Result(column="id",property="id"),@Result(column="net_type",property="netType"),
		@Result(column="net_type_name",property="netTypeName"),@Result(column="ip_segment",property="ipSegment"),
		@Result(column="ip",property="ip"),@Result(column="state",property="state"),
		@Result(column="apply_person",property="applyPerson"),@Result(column="purpose",property="purpose"),
		@Result(column="create_user",property="createUser"),@Result(column="create_date",property="createDate"),
		@Result(column="update_user",property="updateUser"),@Result(column="update_date",property="updateDate"),
		@Result(column="remarks",property="remarks")})
	List<IpAddress> getIpAddressInfo(String netType,String ipSegment,String state,String ip);
	
	@Select("select distinct ip_segment from res_ip_address where (#{0}=0 or net_type = #{0}) ")
	@Results({@Result(column="ip_segment",property="ipSegment")})
	List<JSONObject> getIpSegment(String netType);
	
	@Select("select id,ip,state from res_ip_address where ip = #{0} ")
	@Results({@Result(column="id",property="id"),@Result(column="ip",property="ip"),
		@Result(column="state",property="state")})
	List<JSONObject> getIp(String ip);
	
	@Insert("INSERT INTO res_ip_address (id, net_type, ip_segment, ip, state, create_user, create_date, remarks) "
			+ " values(#{id},#{netType},#{ipSegment},#{ip},#{state},#{createUser},#{createDate},#{remarks})")
	int insertIpAddress(IpAddress ipAddress);

	@Update("update res_ip_address set net_type = #{netType},ip_segment=#{ipSegment},ip =#{ip},update_user =#{updateUser},update_date=#{updateDate},"
			+ " remarks=#{remarks} where id = #{id}")
	int updateIpAddress(IpAddress ipAddress);
	
	@Update("update res_ip_address set state = #{state}, apply_person = #{applyPerson},purpose=#{purpose},update_user =#{updateUser},update_date=#{updateDate} "
			+ " where id = #{id}")
	int updateIpAddressState(IpAddress ipAddress);//allot&release
	
	@Delete("delete from res_ip_address where id = #{id}")
	int deleteIpAddress(Long id);

	@Update("update res_ip_address set state = #{state} where ip = #{ip}")
	int updateIpState(IpAddress ipAddress);
}
